<template>
  <div class="app-container">
    <div v-show="!detailInfo.visible" v-if="$permission('productDate:page')">
      <!-- 条件栏 -->
      <NewPageFilter
        v-model:query="filterInfo.query.queryMap"
        :query-more-json="false"
        :filter-list="filterInfo.searchList"
        :filter-btn="filterInfo.searchBtn"
        :clear-type="clearType"
      ></NewPageFilter>
      <NewPageFilter :filter-btn="filterInfo.btnList">
        <el-button v-show="true" @click="print">打印</el-button>
        <el-button v-show="true" @click="exportExcel">导出excel</el-button>
      </NewPageFilter>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        v-model:query="filterInfo.query.queryMap"
        :field-list="tableInfo.fieldList"
        :tab-index="true"
        :check-box="false"
        @pageInfo="pageInfo"
      >
        <template v-slot:col-commonName="scope">
          <p class="slip-edit" @click="showDetail(scope.row)">
            {{ scope.row.commonName || '-' }}
          </p>
          <p>{{ scope.row.code || '-' }}</p>
          <p>{{ scope.row.alias || '-' }}</p>
        </template>
        <template v-slot:col-ReceivingTime="scope">
          <p>{{ scope.row.minReceivingTime || '-' }}</p>
          <p>{{ scope.row.maxReceivingTime || '-' }}</p>
        </template>
        <template v-slot:col-validityDates="scope">
          <p>{{ scope.row.minValidityDate || '-' }}</p>
          <p>{{ scope.row.maxValidityDate || '-' }}</p>
        </template>
        <template v-slot:col-consumeTime="scope">
          <p>{{ scope.row.consumeTime || '-' }}</p>
        </template>
        <template v-slot:col-updateTime="scope">
          <p>
            {{
              scope.row.status === 'enabled' ? '-' : scope.row.updateTime || '-'
            }}
          </p>
        </template>
      </TableContainer>
    </div>

    <PageDialog
      v-if="dialogTipsInfo.visible"
      :title="dialogTipsInfo.title"
      v-model:visible="dialogTipsInfo.visible"
      :width="dialogTipsInfo.width"
      :bt-loading="dialogTipsInfo.btLoading"
      :bt-list="dialogTipsInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogTipsInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogTipsInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </PageDialog>

    <Details
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :row="detailInfo.row"
      @callback="getCustomize"
    />
  </div>
</template>

<script setup>
import {
  getProductDatePage,
  productDatePrint,
  productDateExcel,
} from '@/api/15189.js'
import Details from './details'

import { fatchTemplateByCode } from '@/api/base'

const { proxy } = getCurrentInstance()
// 过滤相关配置
const dialogTipsInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: true,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '打印',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})
const filterInfo = ref({
  query: {
    groupMap: {},
    groups: [],
    sortString: '',
    queryMap: {
      sort: 'order_no desc',
    },
    customizeUserVO: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
  btnList: [
    {
      type: 'button',
      label: '打印',
      event: print,
      show: proxy.$permission('productDate:print'),
    },
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('productDate:execl'),
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [
    {
      label: '通用名称\n编码\n别名',
      value: 'commonName',
      width: '220px',
      type: 'slot',
    },
    { label: '接收日期', value: 'ReceivingTime', type: 'slot' },
    { label: '失效日期', value: 'validityDates', type: 'slot' },
    { label: '使用日期', value: 'consumeTime', type: 'slot' },
    { label: '停用日期', value: 'updateTime', type: 'slot' },
    { label: '当前状态', value: 'statusValue' },
  ],
})
const detailInfo = ref({
  visible: false,
  row: {},
})
const page = ref({
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
const clearType = ref(true)
function getCustomize() {
  tableInfo.value.api = getProductDatePage
  select()
}
// 查询
function select() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
//导出
function exportExcelList() {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  productDateExcel(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
//打印
function printList() {
  fatchTemplateByCode({
    resourceCode: 'productDate:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  productDatePrint(params).then((resp) => {
    if (resp && resp.success) {
      let data = proxy.$fn.deepClone(resp.data)
      proxy.$lodop.preview(temp, data)
    }
  })
}
function showDetail(row) {
  detailInfo.value.row = row
  detailInfo.value.visible = true
}
function exportExcel() {
  //导出
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'excel'
  dialogTipsInfo.value.msg = '请选择数据导出范围'
  dialogTipsInfo.value.btList[1].label = '导出'
  dialogTipsInfo.value.flag = true
}
function print() {
  //打印
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'print'
  dialogTipsInfo.value.msg = '请选择当数据打印范围'
  dialogTipsInfo.value.btList[1].label = '打印'
  dialogTipsInfo.value.flag = true
}
//打印导出
function pageInfo(obj) {
  page.value = obj
}
function checkConfirm() {
  if (dialogTipsInfo.value.type === 'excel') {
    exportExcelList()
  } else if (dialogTipsInfo.value.type === 'print') {
    printList()
  }
  dialogTipsInfo.value.visible = false
  dialogTipsInfo.value.msg = ''
}
function handleClose() {
  dialogTipsInfo.value.visible = false
}

let obj = {
  commonName: {
    label: '产品名称',
    value: 'commonName',
    type: 'column',
    componentType: 'input',
    event: search,
    hidden: false,
    show: true,
    clearable: true,
  },
  code: {
    label: '产品编码',
    value: 'code',
    type: 'column',
    componentType: 'input',
    event: search,
    hidden: false,
    show: true,
    clearable: true,
  },
  status: {
    label: '状态',
    value: 'status',
    asValue: 'status',
    type: 'column',
    componentType: 'select-model',
    code: 'enabled_type',
    // list: "typeList",
    hidden: false,
    show: true,
    event: search,
    clearable: true,
  },
}

filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
getCustomize()
</script>

<style scoped></style>
